package com.xiaoq.distribution.repository.impl;

import java.util.ArrayList;
import java.util.List;

import com.mongodb.WriteConcern;
import org.apache.commons.lang3.StringUtils;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;
import org.mongodb.morphia.query.Sort;
import org.mongodb.morphia.query.UpdateOperations;
import org.mongodb.morphia.query.UpdateResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xiaoq.coms.util.IdGeneratorUtils;
import com.xiaoq.distribution.entity.DistriTicketEntity;
import com.xiaoq.distribution.repository.IDistriTicketRepository;
import com.xiaoq.vo.PagingVo;

@Repository
public class DistriTicketRepositoryImpl implements IDistriTicketRepository {
	
	@Autowired
	private Datastore datastore;

	@Override
	public boolean save(DistriTicketEntity distriTicket) {
		datastore.save(distriTicket);
		return true;
	}

	@Override
	public DistriTicketEntity findById(String id) {
		Query<DistriTicketEntity> distriTicketQuery = datastore.find(DistriTicketEntity.class).field(IdGeneratorUtils.IDGENERATOR).equal(id);
		return distriTicketQuery.get();
	}

	@Override
	public List<DistriTicketEntity> findDistriTickets(String agreementId, PagingVo pvo) {
		int ps = pvo.getPs();
		int pn = (pvo.getPn() - 1) * ps;
		Long total = 0L;
		List<DistriTicketEntity> distriTicketList = new ArrayList<>();
		Query<DistriTicketEntity> distriTicketQuery = datastore.find(DistriTicketEntity.class).field("agreementId")
				.equal(agreementId);
		total = distriTicketQuery.count();
		distriTicketList = distriTicketQuery.offset(pn).limit(ps).asList();
		pvo.setTc(total);
		return distriTicketList;
	}

	@Override
	public List<DistriTicketEntity> findDistriTicketsByStatus(String shopId, PagingVo pvo, String status) {
		int ps = pvo.getPs();
		int pn = (pvo.getPn() - 1) * ps;
		Long total = 0L;
		List<DistriTicketEntity> distriTicketList = new ArrayList<>();
		Query<DistriTicketEntity> distriTicketQuery = datastore.find(DistriTicketEntity.class).field("shopId")
				.equal(shopId).field("status").equal(status);
		total = distriTicketQuery.count();
		distriTicketList = distriTicketQuery.offset(pn).limit(ps).asList();
		pvo.setTc(total);
		return distriTicketList;
	}

	@Override
	public DistriTicketEntity getDistriTicket(String distribId, String ticketId) {

		Query<DistriTicketEntity> query = datastore.find(DistriTicketEntity.class)
				.order(Sort.descending("createdAt"));

		if(StringUtils.isNotBlank(distribId)){
			query.filter("distribId = ", distribId);
		}

		if(StringUtils.isNotBlank(ticketId)){
			query.filter("ticketId = ", ticketId);
		}

		return query.get();
	}

	@Override
	public List<DistriTicketEntity> getDistriTicketList(String distribId, String ticketId, String status) {
		Query<DistriTicketEntity> query = datastore.createQuery(DistriTicketEntity.class);

		if(StringUtils.isNotBlank(distribId)){
			query.filter("distribId = ", distribId);
		}

		if(StringUtils.isNotBlank(ticketId)){
			query.filter("ticketId = ", ticketId);
		}

		if(StringUtils.isNotBlank(status)){
			query.filter("status = ", status);
		}

		return query.asList();
	}


	@Override
	public boolean updateTicketCommitCount(String id, int plusSellTicketCount) {
		Query<DistriTicketEntity> query = datastore.createQuery(DistriTicketEntity.class)
				.filter("_id", id)
				.where("this.commitAuthNum + " + plusSellTicketCount + " <= " + "this.authNum");

		UpdateOperations updateOperations = datastore.createUpdateOperations(DistriTicketEntity.class);
		updateOperations.inc("commitAuthNum", plusSellTicketCount);
		UpdateResults updateResults = datastore.update(query, updateOperations,
				false, WriteConcern.ACKNOWLEDGED);
		return updateResults.getUpdatedExisting();
	}
}
